package com.quad.innovators.salesease.service.impl.base;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quad.innovators.salesease.mapper.base.ShopBrandProviderMapper;
import com.quad.innovators.salesease.model.entity.base.ShopBrandProvider;
import com.quad.innovators.salesease.service.base.ShopBrandProviderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class ShopBrandProviderServiceImpl extends ServiceImpl<ShopBrandProviderMapper, ShopBrandProvider> implements ShopBrandProviderService {


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(String shopCode, List<String> brandprovideCodeList) {
        // 删除旧的
        this.baseMapper.delete(new LambdaQueryWrapper<ShopBrandProvider>()
                .eq(ShopBrandProvider::getShopCode, shopCode));
        // 新增新的
        Set<ShopBrandProvider> shopBrandProviderSet = brandprovideCodeList.stream().map(it -> {
            ShopBrandProvider shopBrandProvider = new ShopBrandProvider();
            shopBrandProvider.setShopCode(shopCode);
            shopBrandProvider.setBrandProviderCode(it);
            return shopBrandProvider;
        }).collect(Collectors.toSet());
        this.saveBatch(shopBrandProviderSet);
        return true;
    }

    @Override
    public List<String> selectListByShopCode(String shopCode) {
        List<ShopBrandProvider> shopBrandProviders = this.baseMapper.selectList(new LambdaQueryWrapper<ShopBrandProvider>()
                .eq(ShopBrandProvider::getShopCode, shopCode)
                .select(ShopBrandProvider::getBrandProviderCode));
        return shopBrandProviders.stream().map(ShopBrandProvider::getBrandProviderCode).collect(Collectors.toList());
    }
}
